package Cantella::DJabberd::Types;

use DJabberd::JID;
use MooseX::Types::Moose qw/Object Str/;
use MooseX::Types -declare => [qw/JID JIDNode JIDDomain JIDResource/];

subtype JID,
  as Object,
  where { $_->isa('DJabberd::JID') },
  message { "Must be a DJabberd::JID instance" };

coerce JID,
  from 'Str',
  via { DJabberd::JID->new($_) };

subtype JIDNode,
  as Str,
  where {$_ =~ /^[\x29\x23-\x25\x28-\x2E\x30-\x39\x3B\x3D\x3F\x41-\x7E]{1,1023}$/},
  message { "JIDNode must be an escaped string between 1 and 1023 bytes" };

subtype JIDDomain,
  as Str,
  where {$_ =~ /^[a-zA-Z0-9\.\-]{1,1023}$/},
  message { "JIDDomain must be a valid string between 1 and 1023 bytes" };

subtype JIDResource,
  as Str,
  where {$_ =~ /^.{1,1023}$/},
  message { "JIDResource must be a valid string between 1 and 1023 bytes" };

